package com.abaaba.project2.controller.wxcontroller;

import com.abaaba.project2.bean.User;
import com.abaaba.project2.bean.bo.WxLoginBo;
import com.abaaba.project2.bean.bo.WxRegisterBo;
import com.abaaba.project2.bean.bo.wx.WxResetBo;
import com.abaaba.project2.bean.vo.BaseRespVo;
import com.abaaba.project2.bean.vo.wx.WxLoginVo;
import com.abaaba.project2.bean.vo.wx.WxUserLoginInfoVo;
import com.abaaba.project2.configuration.AliyunComponent;
import com.abaaba.project2.mapper.UserMapper;
import com.abaaba.project2.service.WxAuthService;
import com.abaaba.project2.shiro.Token.CustomToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/wx/auth")
public class WxAuthController {
    public Subject subject;

    @Autowired
    WxAuthService wxAuthService;

    @Autowired
    AliyunComponent aliyunComponent;

    @RequestMapping("/login")
    public BaseRespVo wxLogin(@RequestBody WxLoginBo wxLoginBo) {
        subject = SecurityUtils.getSubject();

        CustomToken customToken = new CustomToken(wxLoginBo.getUsername(), wxLoginBo.getPassword(), "wx");

        try {
            subject.login(customToken);

            WxLoginVo wxLoginVo = new WxLoginVo();
            wxLoginVo.setToken(subject.getSession().getId());
            wxLoginVo.setTokenExpire("2021-12-08T22:54:28.637");
            User user = (User) subject.getPrincipal();
            wxLoginVo.setUserInfo(new WxUserLoginInfoVo(user.getAvatar(), user.getNickname()));

            return BaseRespVo.ok(wxLoginVo);
        }
        catch (Exception e)
        {
            return BaseRespVo.fail("账号或密码错误");
        }
    }

    @RequestMapping("/logout")
    public BaseRespVo wxLogout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok();

    }

    @RequestMapping("/regCaptcha")
    public BaseRespVo wxRegCaptcha(@RequestBody Map phoneNumber) {
        int i = new Random().nextInt(1000000);
        try {
            aliyunComponent.sendMsg((String) phoneNumber.get("mobile"), String.valueOf(i));
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.fail();
        }
        wxAuthService.insertVerifyCode((String) phoneNumber.get("mobile"), String.valueOf(i));
        return BaseRespVo.ok();
    }

    @RequestMapping("/register")
    public BaseRespVo register(@RequestBody WxRegisterBo wxRegisterBo) {
        if (!wxAuthService.selectCodeByMobile(wxRegisterBo.getMobile()).equals(wxRegisterBo.getCode())) {
            return BaseRespVo.fail(703,"验证码错误！");
        }
        String s = wxAuthService.verifyUsername(wxRegisterBo.getUsername());
        if (StringUtils.hasLength(s)) {
            return BaseRespVo.fail(703,"用户名已经存在！");
        }
        User user = wxAuthService.selectUserByMobile(wxRegisterBo.getMobile());
        if (user != null) {
            return BaseRespVo.fail(703,"该手机号已经注册！");
        }
        try {
            wxAuthService.insertUser(wxRegisterBo);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok("成功！");
    }

    @RequestMapping("/reset")
    public BaseRespVo reset(@RequestBody WxResetBo wxResetBo) {
        if (!wxAuthService.selectCodeByMobile(wxResetBo.getMobile()).equals(wxResetBo.getCode())) {
            return BaseRespVo.fail(703,"验证码错误！");
        }
        User user = wxAuthService.selectUserByMobile(wxResetBo.getMobile());
        if (user == null) {
            return BaseRespVo.fail(703,"用户不存在！");
        }
        try {
            wxAuthService.updateUser(user, wxResetBo);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok("成功！");
    }

}
